﻿using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using Camc.Quality.DataExporting.Excel.EpPlus;
using Camc.Quality.Dto;
using Camc.Quality.Storage;
using Camc.Quality.TrepanningManager.BomAnalyseDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.TrepanningManager.BomNestingInterface
{
   public class BomAnalyseExportToExcel: EpPlusExcelExporterBase, IBomAnalyseExportToExcel
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;

        public BomAnalyseExportToExcel(
            ITimeZoneConverter timeZoneConverter,
            IAbpSession abpSession,
            ITempFileCacheManager tempFileCacheManager)
            : base(tempFileCacheManager)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }

        public string ExportToFile(List<BomAnalyseContentDto>  bomAnalyseContentDtos)
        {
            return CreateExcelPackageLocal(
               "预配套明细表.xlsx",
               excelPackage =>
               {
                   var sheet = excelPackage.Workbook.Worksheets.Add(L("预配套明细"));
                   sheet.OutLineApplyStyle = true;

                   AddHeader(
                       sheet,
                        L("物料代码"),
                        L("物料名称"),
                        L("规格"),
                        L("年代号"), //add 12/1
                        L("物料类别"),
                        L("细类"),
                        L("单位"),
                        L("芯级用量"),
                        L("自由库存数量"),
                        L("可用库存数量"),
                        L("预配套数量"),
                        L("预配调整数"),
                        L("缺料数量"),
                        L("出库数量"),
                        L("预配套状态"),
                        L("预配套人"),
                        L("预配套时间")
                       );

                   AddObjects(
                       sheet, 2, bomAnalyseContentDtos,
                        _ => _.MaterialNo,
                        _ => _.MaterialName,
                        _ => _.Module,
                        _ => _.YearsNo, //add 12/1
                        _ => _.MaterialTypeName,
                        _ => _.MaterialCategoryName,
                        _ => _.UnitName,
                        _ => _.BomDosageTotalAmount,
                        _ => _.FreeQuantityStock,
                        _ => _.UsableQuantityStock,
                        _ => _.PreSuiteStock,
                        _ => _.CompleteAdjust,
                        _ => _.StarvingStock,
                        _ => _.DeliveryStock,
                        _ => _.PreSuiteStatusName,
                        _=>_.PreSuiteName,
                        _ => _.PreSuiteTime
                       //_ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
                       );
                   //Formatting cells

                   for (var i = 1; i <= 16; i++)
                   {
                       sheet.Column(i).AutoFit();
                   }
                   var creationTimeColumn = sheet.Column(15);
                   creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd HH:mm:ss";

               });
        }
    }
}
